/*
 * Time.java
 * Created on Apr 18, 2005 3:09:09 PM
 */
package edu.mobbuzz.messaging;

import java.util.Calendar;


/**
 * @author Andry
 * The Time.java class provides
 *
 */
public class Time implements Runnable {
    
    
    private static final int UPDATE_INTERVAL = 10;	// Time updated interval in second
    // private static final int INITIAL_COUNTER = 1000;
    
    //private Messaging messaging;
    public TimeStamp current;
    private boolean run;
    public int sleepInterval;						// Sleep interval in millisecond
    // private int counter;

    /**
     * @param messaging		Messaging instance that using this class
     */
    public Time(/*Messaging messaging*/) {
        //this.messaging	= messaging;
        sleepInterval 	= UPDATE_INTERVAL * 1000;
        // counter 		= INITIAL_COUNTER;
        run 			= false;
    }   
    
    /* (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    public void run() {
        run = true;
        while (run) {
            try
            {
              Thread.sleep( sleepInterval );
            } catch( InterruptedException e ) {
                continue;
            } 
            advanceTime(UPDATE_INTERVAL);
            /*if (--counter == 0) {
                messaging.synchronizeTime();
                counter = INITIAL_COUNTER;
            }*/
        }
    }
    
    /**
     * Stop current thread, unblocking
     */
    public void stop() {
        run = false;
    }
    
    /**
     * Advance current time in Time object by given seconds
     * @param seconds	Seconds to advance
     */
    private void advanceTime(int seconds) {
        this.current.second += seconds;
        while (this.current.second > 59) {
            this.current.second -= 60;
            this.current.minute++;
            if (this.current.minute > 59) {
                this.current.minute = 0;
                this.current.hour++;
                if (this.current.hour > 23) {
                    this.current.hour = 0;
                    if (this.current.day == 31) {
                    	this.current.day = 1;
                        if (this.current.month == 12) {
                        	this.current.month = 1;
                        	this.current.year++;
                        } else {                    
                        	this.current.month++;
                        }
                    } else if (this.current.day == 30) {
                        if ((this.current.month == 4) || (this.current.month == 6) || (this.current.month == 9) || (this.current.month == 11)) {
                        	this.current.day = 1;
                            this.current.month++;
                        } else {
                        	this.current.day = 31;
                        }
                    } else if ((this.current.day == 28) && (this.current.month == 2)) {
                        if ((this.current.year % 4 == 0) && ((this.current.year % 100 != 0) || (this.current.year % 400 == 0))) {
                        	this.current.day = 29;
                        } else {
                        	this.current.day = 1;
                            this.current.month = 3;
                        }
                    } else if ((this.current.day == 29) && (this.current.month == 2)) {
                    	this.current.day = 1;
                        this.current.month = 3;
                    } else {
                    	this.current.day++;
                    }
                }
            }
        }
    }
    
    /**
     * @return current time in format YYYYMMDDhhmmss GMT
     */
    public String getCurrentTime() {
        if (current != null) {
            return current.toStringUnformatted();
        } else {
            Calendar current = Calendar.getInstance();
            
            return (TimeStamp.normalizeTime(new TimeStamp(current.get(Calendar.YEAR),
                    current.get(Calendar.MONTH) + 1,
                    current.get(Calendar.DATE),
                    current.get(Calendar.HOUR_OF_DAY) - Messaging.GMT_TIMEZONE,
                    current.get(Calendar.MINUTE),
                    current.get(Calendar.SECOND),
					0
                    ))).toStringUnformatted();
            
        }
    }
}
